<%@ page errorPage="../jsp-grad/AppError.jsp" import="com.gedas.internal.grad.db.*, com.gedas.internal.grad.security.Eldap" %>

<jsp:useBean id="mySecurity" scope="session" class="com.gedas.internal.grad.security.User" />

<%  String netId = mySecurity.getNetid();
    // Get the context to obtain global variables
    ServletContext context = getServletContext();
    String adminMode =  context.getInitParameter("adminMode");

    if (netId == null){
        String useNTLM = context.getInitParameter("useNTLM");
        
        // If NTLM is activated ask for the user Logged in
        if (useNTLM.equals("true")){
            netId = request.getRemoteUser();
            if (netId.indexOf((char)92) > 0){
                netId = netId.substring(netId.indexOf((char)92) + 1, netId.length());
            }
            // Add the user to the database
            Eldap myLdap = new Eldap();
            myLdap.setInitctx(context.getInitParameter("INITCTX"));
            myLdap.setHost(context.getInitParameter("LDAP_HOST"));
            myLdap.setUser(context.getInitParameter("LDAP_USER"));
            myLdap.setSearchbase(context.getInitParameter("LDAP_SEARCHBASE"));
            myLdap.addUser(netId);

            mySecurity.setNetid(netId);
        }
    }

    // Check again for the netId the session exists
    if (netId == null){
        // LDAP AUTHENTICATION

        // -----------------------------------------------------------------------------------
        // The session does not exists and is not the local IP, we need to check on the cookie
        // to verify if this user visited the site before so we can get the netId
        // -----------------------------------------------------------------------------------
        Cookie[] theCookies = request.getCookies();
        if (theCookies != null){
            for (int i = 0; i < theCookies.length; i++){
                //theCookies[i].setPath(request.getContextPath());
                String cookieName = theCookies[i].getName();
                if (theCookies[i] != null){
                    out.println(cookieName);
                     if (cookieName.equals("userIdentifier")){
                        
                        // The cookie exists on the client. 
                        // Then we don't need to authenticate against LDAP

                        // Get the value of the cookie
                        String userIdentifier = theCookies[i].getValue();

                        // Get the netId that correspons to this Identifier
                        try {
                            netId = mySecurity.getNetidByIdentifier(userIdentifier);
                        } catch (Exception e) {
                            throw new ServletException("Error while reading the netId from the database: " + e);
                        }
                        mySecurity.setNetid(netId);
                    }
                }
            }
        }

        if (netId == null){
            // If netId is still null that means that there is no session and no
            // cookie either. We need to authenticate the user against LDAP
%>
            <jsp:forward page="../jsp-grad/login.jsp" />
<%      } else {
            // There was a cookie on the client.
            // We need to stablish the netId in the bean
            mySecurity.setNetid(netId);
        }
    }

    // At this point the user is authenticated.
    // We now have to create a connection to the DB
    DbConnection dbCon = new DbConnection();

    // Create the page
    try {
        // Connect to the database
        dbCon.connect();
%>
